package com.amsp.oa.oademo.controller;

import com.amsp.oa.oademo.entity.FixedAssetTransfer;
import com.amsp.oa.oademo.service.FixedAssetTransferService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/fixed-asset-transfer")
@CrossOrigin
@Api(tags = "固定资产转移")
public class FixedAssetTransferController {

    @Autowired
    private FixedAssetTransferService fixedAssetTransferService;

    @PostMapping("/save")
    @ApiOperation("保存固定资产转移申请")
    public boolean save(@RequestBody FixedAssetTransfer fixedAssetTransfer) {
        LocalDateTime now = LocalDateTime.now();
        if (fixedAssetTransfer.getId() != null) {
            // 更新
            fixedAssetTransfer.setUpdateTime(now);
            return fixedAssetTransferService.updateById(fixedAssetTransfer);
        } else {
            // 新增
            fixedAssetTransfer.setCreateTime(now);
            fixedAssetTransfer.setUpdateTime(now);
            return fixedAssetTransferService.save(fixedAssetTransfer);
        }
    }

    @GetMapping("/list")
    @ApiOperation("分页查询固定资产转移申请")
    public Page<FixedAssetTransfer> list(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String transferNo,
            @RequestParam(required = false) String assetName,
            @RequestParam(required = false) String assetNumber,
            @RequestParam(required = false) String outDepartment,
            @RequestParam(required = false) String inDepartment,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        return fixedAssetTransferService.getPage(current, size, transferNo, assetName,
                assetNumber, outDepartment, inDepartment, startDate, endDate);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询固定资产转移申请")
    public FixedAssetTransfer getById(@PathVariable Long id) {
        return fixedAssetTransferService.getById(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("根据ID删除固定资产转移申请")
    public boolean delete(@PathVariable Long id) {
        return fixedAssetTransferService.removeById(id);
    }
}